<template>
  <t-row :gutter="[16, 16]">
    <t-col v-for="(item, index) in panelData" :key="item.title" :xs="6" :xl="3">
      <t-card
        :title="t(item.title)"
        :bordered="false"
        :class="{ 'dashboard-item': true, 'dashboard-item--main-color': index == 0 }"
      >
        <div class="dashboard-item-left">
          <t-statistic
            ref="refUps"
            :value="item.number"
            :animation="{
              valueFrom: 0,
              duration: 1500,
            }"
          />
        </div>
        <div class="dashboard-item-right">
          <span><t-icon :name="item.icon"></t-icon></span>
        </div>
      </t-card>
    </t-col>
  </t-row>
</template>

<script lang="ts">
export default {
  name: 'DashboardTopPanel',
};
</script>

<script setup lang="ts">
import { nextTick, ref, watch } from 'vue';

import { t } from '@/locales';

import { PANEL_DATA } from '../constants';

const props = defineProps({
  data: {
    type: Object,
    default: () => {},
  },
});

const panelData = ref<Recordable[]>([...PANEL_DATA]);

const refUps = ref();
watch(
  () => props.data,
  (val) => {
    const list = [val.document, val.category, val.tag, val.attachement];
    panelData.value = PANEL_DATA.map((v, k) => {
      v.number = list[k];
      return v;
    });

    nextTick(() => {
      refUps.value.forEach((v: { start: () => void }) => {
        v.start();
      });
    });
  },
);
</script>

<style lang="less" scoped>
.dashboard-item {
  padding: var(--td-comp-paddingTB-xl) var(--td-comp-paddingLR-xxl);

  :deep(.t-card__header) {
    padding: 0;
  }

  :deep(.t-card__footer) {
    padding: 0;
  }

  :deep(.t-card__title) {
    font: var(--td-font-body-medium);
    color: var(--td-text-color-secondary);
  }

  :deep(.t-card__body) {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex: 1;
    position: relative;
    padding: 0;
    margin-top: var(--td-comp-margin-s);
    margin-bottom: var(--td-comp-margin-xxl);
  }

  &:hover {
    cursor: pointer;
  }

  &-left {
    display: flex;
    flex-direction: row;
    align-items: flex-start;

    :deep(.t-statistic) span {
      color: var(--td-text-color-primary);
      font-size: var(--td-font-size-headline-large);
      line-height: var(--td-line-height-headline-medium);
    }
  }

  &-right {
    position: absolute;
    top: 0;
    right: 0;

    > span {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      width: var(--td-comp-size-xxxl);
      height: var(--td-comp-size-xxxl);
      background: var(--td-brand-color-light);
      border-radius: 50%;

      .t-icon {
        font-size: 24px;
        color: var(--td-brand-color);
      }
    }
  }

  // 针对第一个卡片需要反色处理
  &--main-color {
    background: var(--td-brand-color);
    color: var(--td-text-color-primary);

    :deep(.t-card__title),
    .dashboard-item-left :deep(.t-statistic) span {
      color: var(--td-text-color-anti);
    }
  }
}
</style>
